var checkflag = false;
function loadGird(frm){
		    reloadGrid(frm, 1);
	 		$("#"+frm).submit(function(){
	    		reloadGrid(frm,1);
	    	    return false;
	    	});	 		
}
	
function reloadGrid(frm, pageNumber){
    var param = $("#"+frm).serialize();
    var loading=$("#"+frm+" .loading");
	$.ajax({
		url :$("#"+frm).attr("action"), //要访问的后台地址
	    data: param + "&currentPage=" + pageNumber,//要发送的数据
	    type : 'POST', //使用get方法访问后台
	    dataType : 'json', //返回json格式的数据
	    beforeSend:function(){
	     	loading.show();	
	    },
	    success : function(data){
	    	$("#"+frm+" .grid tbody tr:not(:first)").remove();  // 删除除第一行外的所有行  
   			$("#"+frm+" .grid tbody tr:eq(0)").show();  //将模板显示出来
   			var rows = data.recordList; 
   			//var rows = data;
	    	eval("callback_"+frm)(rows, frm);
	    	$("#"+frm+" .grid tbody tr:eq(0)").hide();   //将模板隐藏起来
	    	$("#"+frm+" .grid tbody tr:not(:first)").click(function(){
		   		if(!checkflag){
		    		$("#"+frm+" .grid tbody tr:not(:first)").attr("class","tr_nbw");
			        $("#"+frm+" .grid tbody tr").find(".klId").attr("checked", false);
			        $(this).find(".klId").attr("checked", true);
			        $(this).find(".klId").trigger("click");
			        $(this).find(".klId").attr("checked", true);
			        $("#"+frm+" .grid thead tr td .allCheck").attr("checked", false);
		       	}
		   		checkflag=false;
		     }); 
		     
		     $("#"+frm+" .grid tbody tr:not(:first) td .klId").click(function(){
		     	if($(this).attr("type")=="radio"){
		     		$("#"+frm+" .grid tbody tr:not(:first)").attr("class","tr_nbw");
		     	}
		     	checkflag=true;
		        if($(this).attr("checked")==true){
		        	$( this ).parent().parent().attr("class","tr_select_nbw");
			   		var flag=true;
			   		$("#"+frm+" .grid tbody tr:not(:first) td .klId").each(function(){
						if(!this.checked){flag=false;}
					});
					if(flag){
						$("#"+frm+" .grid thead tr td .allCheck").attr("checked", true);
					}
		   		}else{
		   			$( this ).parent().parent().attr("class","tr_nbw");
		   			$("#"+frm+" .grid thead tr td .allCheck").attr("checked", false);
		   		}
		     }); 
		     
		     $("#"+frm+" .grid thead tr td .allCheck").click(function(){
		     	$("#"+frm+" .grid tbody tr:not(:first)").each(function(){
						if(!$(this).find(".klId").attr("checked") && $("#"+frm+" .grid thead tr td .allCheck").attr("checked")){
							$(this).find(".klId").attr("checked", true);
							$(this).find(".klId").trigger("click");
							$(this).find(".klId").attr("checked", true);
						}
						if($(this).find(".klId").attr("checked") && !$("#"+frm+" .grid thead tr td .allCheck").attr("checked")){
							$(this).find(".klId").attr("checked", false);
							$(this).find(".klId").trigger("click");
							$(this).find(".klId").attr("checked", false);
						}
				});
		     }); 

             //生成分页
           createPagination(data.allRow, data.pageSize, data.currentPage, frm );
           //createPagination(10, 5, 1, frm );
	    },
	    complete:function(){
	     loading.hide();	
	    }
	});	
}


function isNumber(s)   
{   
	var pageSize = s;
	var patrn=/^[0-9]*[1-9][0-9]*$/; 
	if (!patrn.exec(pageSize.value)){ 
		alert("请输入大于1的整数。");
		pageSize.value="1";
		return false
	}  
	return true  
}  

function createPagination(doccount,pcount,curpage, frm){
	var inputTxt = "每页显示条数： <input type='text' size='3' onkeyup='isNumber(this)' id='pageSize' name='pageSize' value='"+pcount+"'>";
	var goInput="&nbsp;<label><input name='goto' id='goto' type='submit' value='GO' ></label>";
	inputTxt+=goInput;
	if(doccount>0){
  		var strhtml = "<div id=page>";
  		var spoint = (curpage-1)*pcount+1;
 	 	if(doccount<=pcount){
      			strhtml += "第"+spoint+"－"+doccount+"条/共"+doccount+"条&nbsp;&nbsp;";
      			strhtml += "第<font color=#ff0000>1</font>页/共1页&nbsp;&nbsp;";
      			strhtml += inputTxt + "&nbsp;&nbsp;";
       	}
    	else{
      			var epoint = spoint + pcount - 1;
      			var curpage = epoint/pcount;
      			var pagesum;
      			var count = doccount%pcount;
      			if(count==0)
        			pagesum = doccount/pcount;
      			else
        			pagesum = (doccount-count)/pcount+1;
      			if(curpage==pagesum)
        			strhtml += "第"+spoint+"－"+doccount+"条/共"+doccount+"条&nbsp;&nbsp;";
      			else
        			strhtml += "第"+spoint+"－"+(pcount*curpage)+"条/共"+doccount+"条&nbsp;&nbsp;";
      			strhtml += "第<font color=red>"+curpage+"</font>页/共"+pagesum+"页&nbsp;&nbsp;";
      			if(curpage==1){
        			strhtml += "<font color=#aaaaaa>首页</font>&nbsp;&nbsp;";
        			strhtml += "<font color=#aaaaaa>上一页</font>&nbsp;&nbsp;";
         		}
      			else{
        			strhtml += "<a href=javascript:reloadGrid('"+frm+"',1)>首页</a>&nbsp;&nbsp;";
        			strhtml += "<a href=javascript:reloadGrid('"+frm+"','"+(curpage-1)+"')>上一页</a>&nbsp;&nbsp;";
          		}
      			if(curpage==pagesum){
        			strhtml += "<font color=#aaaaaa>下一页</font>&nbsp;&nbsp;";
        			strhtml += "<font color=#aaaaaa>尾页</font>&nbsp;&nbsp;";
        	 	}
      		
      			else{
        			strhtml += "<a href=javascript:reloadGrid('"+frm+"','"+(curpage+1)+"')>下一页</a>&nbsp;&nbsp;";
        			strhtml += "<a href=javascript:reloadGrid('"+frm+"','"+pagesum+"')>尾页</a>&nbsp;&nbsp;";
        			
          		}

      			strhtml += inputTxt + "&nbsp;&nbsp;";
      			strhtml += "跳转至：<select onchange=reloadGrid('"+frm+"',this.options[this.selectedIndex].value)>"
      			for(i=1;i<=pagesum;i++){
        			if(curpage==i)
          				strhtml += "<option selected value="+i+">第"+i+"页</option>";
        			else
          				strhtml += "<option value="+i+">第"+i+"页</option>";
             	}
      			strhtml += "</select>&nbsp;";
     	}
 	 	//strhtml +=goInput;
     	strhtml += "</div>";
    	//document.write(strhtml);
    	$("#"+frm+" .pagediv").html(strhtml);
	}
}
